//
//  HMVisitorView.swift
//  Weibo-walker
//
//  Created by 郭彬 on 16/3/3.
//  Copyright © 2016年 guobin. All rights reserved.
//

import UIKit

////定代理方法协议
//@objc protocol HMVisitorViewDelegate :NSObjectProtocol{
//    
//    optional func visitorView(visitorView:HMVisitorView,loginButtonDidClicked:UIButton)
//}




class HMVisitorView: UIView {


    //声明一个代理属性
//    @objc var delegate:HMVisitorViewDelegate?
//    //实现协议的方法
//    private func loginButtonClick(button: UIButton) {
//        delegate?.visitorView?(self, loginButtonDidClicked: button)
//    }
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        setupUI()
        startAnimation()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func setPageInfo(imageNamed: String?, message: String?){
        
        if imageNamed == nil{
            startAnimation()
        }
        else{
            circleView.hidden = true
            messageLabel.text = message
            iconView.image = UIImage(named:imageNamed!)
        }
    }
    
    //创建动画
    private func startAnimation(){
        
        let anmi = CABasicAnimation(keyPath: "transform.rotation")
        
        anmi.toValue = 2 * M_PI
        anmi.repeatCount = MAXFLOAT
        anmi.duration = 20
        anmi.removedOnCompletion = false
        circleView.layer.addAnimation(anmi, forKey: nil)
    }
    
    func setupUI(){
        
        // 1. 添加子控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        // 2. 添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        /**
        参数:
        - 1. 要约束的控件
        - 2. 约束的属性,比如中心,左边
        - 3. 相等,大于,小于 (关系)
        - 4. 相对谁来约束
        - 5. 约束的属性,比如中心,左边
        - 6. 乘以
        - 7. 偏移量
        */
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 2.2 圆圈
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        // 2.3 消息label
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        // 2.4 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        // 2.5 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        
        // 2.6 遮罩
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
    }
    
    //懒加载各种控件
    
    //懒加载iconView
    lazy var iconView:(UIImageView) = (UIImageView)(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //懒加载圆圈
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //懒加载label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        // 行数
        label.numberOfLines = 0
        // 居中
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜"
        return label
    }()

    // 注册
    lazy var registerButton: UIButton = {
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        // 添加点击事件
//        button.addTarget(self, action: "loginButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        return button;
    }()
    
    
    // 登录
    lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        // 添加点击事件
//        button.addTarget(self, action: "loginButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        return button;
    }()
    
    // 遮罩
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}



